home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / atari / c / nos042_s / ppp.c < prev    next >
C/C++ Source or Header  |  1994-09-16  |  23KB  |  882 lines

  1. /*
  2.  *  PPP.C    -- Send and receive datagrams on serial lines with
  3.  *           Point-to-Point Protocol
  4.  *
  5.  *    This implementation of PPP is declared to be in the public domain.
  6.  *
  7.  *    Based (in part) upon previous implementations by:
  8.  *    1989    -- Drew Daniel Perkins        (ddp@andrew.cmu.edu)
  9.  *           Carnegie Mellon University
  10.  *    09-90    -- Katie Stevens        (dkstevens@ucdavis.edu)
  11.  *           UC Davis, Computing Services
  12.  *
  13.  *    Jan 91    Bill_Simpson@um.cc.umich.edu
  14.  *        Computer Systems Consulting Services
  15.  *
  16.  *    Feb 91    Glenn McGregor            (ghm@merit.edu)
  17.  *        Testing and suggestions.
  18.  *
  19.  *    May 91    Bill Simpson & Glenn McGregor
  20.  *        Update to newest LCP and IPCP draft RFCs.
  21.  *        Add quick installation features.
  22.  *        Add support for echo and discard message sending.
  23.  *
  24.  *    Jul 91    Glenn McGregor & Bill Simpson
  25.  *        Improve PAP user interface and fix related bugs.
  26.  *        Remove pwaits and "phase machine".
  27.  *
  28.  *  ATARI Version by David Nash - dnash@chaos.demon.co.uk
  29.  *
  30.  *      __stdargs ppp_recv
  31.  *        Include st_asy.h in place of 8250.h
  32.  */
  33.  
  34. /****************************************************************************
  35. *    $Id: ppp.c 1.5 93/04/12 18:05:20 ROOT_DOS Exp $
  36. *    10 Aug 92    1.2    GT    Idle timer.    
  37. *                    1.3    MT    "time auto".
  38. *    21 Sep 92    1.4    GT    Pass address of PPP control structure to try_set_time
  39. *    12 Apr 93    1.5    GT    Only try to set the time when we are running    TCP.
  40. *
  41. *    ATARI version by David Nash - dnash@chaos.demon.co.uk
  42. *
  43. *        Include st_asy.h in place of 8250.h
  44. *
  45. ****************************************************************************/
  46.  
  47. #include "global.h"
  48. #include "mbuf.h"
  49. #include "proc.h"
  50. #include "iface.h"
  51. #include "internet.h"
  52. #include "ip.h"
  53. #include "slhc.h"
  54. #ifdef ATARI
  55. #include "st_asy.h"
  56. #else
  57. #include "8250.h"
  58. #endif
  59. #include "asy.h"
  60. #include "pktdrvr.h"
  61. #include "socket.h"
  62. #include "devparam.h"
  63. #include "ppp.h"
  64. #include "pppfsm.h"
  65. #include "ppplcp.h"
  66. #include "ppppap.h"
  67. #include "pppipcp.h"
  68. #include "trace.h"
  69. #include    "commands.h"
  70.  
  71. extern int done_set_time;
  72.  
  73. /* Routines local to this file */
  74. static struct mbuf *htonppp __ARGS((struct ppp_hdr *ppp, struct mbuf *data));
  75.  
  76. static void ppp_log __ARGS((struct ppp_s *ppp_p, char *comment));
  77. static void ppp_error __ARGS((struct ppp_s *ppp_p, struct mbuf *bp, char *comment));
  78. static void ppp_skipped __ARGS((struct ppp_s *ppp_p, struct mbuf *bp, char *comment));
  79.  
  80. static int ppp_raw __ARGS((struct iface *ifp, struct mbuf *data));
  81.  
  82. static void __stdargs ppp_recv __ARGS((int dev, void *p1, void *p2));
  83.  
  84. static int ppp_iostatus __ARGS((struct iface *ifp, int command, int32 value));
  85. static int ppp_discard     __ARGS((struct iface *ifp, struct mbuf *bp));
  86. static int ppp_echo    __ARGS((struct iface *ifp, struct mbuf *bp));
  87.  
  88. static void reset_idle __ARGS ((struct ppp_s *ppp_p, int16 protocol));
  89.  
  90.  
  91. /*
  92.  * FCS lookup table as generated by fcsgen.c
  93.  */
  94. static int16 fcstab[256] = {
  95.     0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
  96.     0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
  97.     0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
  98.     0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
  99.     0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
  100.     0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
  101.     0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
  102.     0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
  103.     0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
  104.     0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
  105.     0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
  106.     0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
  107.     0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
  108.     0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
  109.     0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
  110.     0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
  111.     0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
  112.     0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
  113.     0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
  114.     0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
  115.     0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
  116.     0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
  117.     0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
  118.     0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
  119.     0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
  120.     0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
  121.     0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
  122.     0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
  123.     0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
  124.     0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
  125.     0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
  126.     0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
  127. };
  128.  
  129. #define pppfcs(fcs, c)        ((fcs >> 8) ^ fcstab[(fcs ^ c) & 0x00ff])
  130. #define SP_CHAR            0x20
  131.  
  132.  
  133. /****************************************************************************/
  134.  
  135. /* Convert PPP header in host form to network form */
  136. static struct mbuf *
  137. htonppp(ppp, data)
  138. struct ppp_hdr *ppp;
  139. struct mbuf *data;
  140. {
  141.     struct mbuf *bp;
  142.     register char *cp;
  143.  
  144.     /* Prepend header onto packet data */
  145.     if ((bp = pushdown(data, PPP_HDR_LEN)) == NULLBUF) {
  146.         free_p( data );
  147.         return NULLBUF;
  148.     }
  149.  
  150.     /* Load header with proper values */
  151.     cp = bp->data;
  152.     *cp++ = ppp->addr;
  153.     *cp++ = ppp->control;
  154.     cp = put16(cp,ppp->protocol);
  155.  
  156.     return bp;
  157. }
  158.  
  159.  
  160. /************************************************************************/
  161. /* General log routine */
  162. static void
  163. ppp_log( ppp_p, comment )
  164. struct ppp_s *ppp_p;
  165. char *comment;
  166. {
  167.     if (ppp_p->trace)
  168.         trace_log(ppp_p->iface,"%s PPP %s",
  169.             ppp_p->iface->name,
  170.             comment);
  171. }
  172.  
  173.  
  174. /* Bad packet */
  175. static void
  176. ppp_error(ppp_p, bp, comment)
  177. struct ppp_s *ppp_p;
  178. struct mbuf *bp;
  179. char *comment;
  180. {
  181.     free_p(bp);
  182.     ppp_log( ppp_p, comment );
  183. }
  184.  
  185.  
  186. /* Unknown type input packet */
  187. static void
  188. ppp_skipped(ppp_p, bp, comment)
  189. struct ppp_s *ppp_p;
  190. struct mbuf *bp;
  191. char *comment;
  192. {
  193.     struct ipcp_s *ipcp_p;
  194.  
  195.     if ( (ipcp_p = ppp_p->fsm[IPcp].pdv) != NULL ) {
  196.         slhc_toss( ipcp_p->slhcp );
  197.     }
  198.     ppp_error( ppp_p, bp, comment );
  199. }
  200.  
  201.  
  202. /****************************************************************************/
  203. /* Send IP datagram with Point-to-Point Protocol */
  204. int
  205. ppp_send(bp,ifp,gateway,prec,del,tput,rel)
  206. struct mbuf *bp;    /* Buffer to send */
  207. struct iface *ifp;    /* Pointer to interface control block */
  208. int32 gateway;        /* Ignored (PPP is point-to-point) */
  209. int prec;
  210. int del;
  211. int tput;
  212. int rel;
  213. {
  214.     struct ppp_s *ppp_p;
  215.     struct ipcp_s *ipcp_p;
  216.     int protocol = PPP_IP_PROTOCOL;
  217.  
  218.     if (ifp == NULLIF
  219.      || (ppp_p = ifp->edv) == NULL) {
  220.         free_p(bp);
  221.         return -1;
  222.     }
  223.  
  224.     if (ppp_p->fsm[IPcp].state != fsmOPENED) {
  225.         ppp_error( ppp_p, bp, "not open for IP traffic" );
  226.         ppp_p->OutError++;
  227.         return -1;
  228.     }
  229.  
  230.     ipcp_p = ppp_p->fsm[IPcp].pdv;
  231.     if (ipcp_p->remote.work.negotiate & IPCP_N_COMPRESS) {
  232.         /* Attempt IP/TCP header compression */
  233.         switch ( slhc_compress(ipcp_p->slhcp, &bp,
  234.             ipcp_p->remote.work.slot_compress) ) {
  235.         case SL_TYPE_IP:
  236.             protocol = PPP_IP_PROTOCOL;
  237.             break;
  238.         case SL_TYPE_COMPRESSED_TCP:
  239.             protocol = PPP_COMPR_PROTOCOL;
  240.             break;
  241.         case SL_TYPE_UNCOMPRESSED_TCP:
  242.             protocol = PPP_UNCOMP_PROTOCOL;
  243.             break;
  244.         default:
  245.             ppp_error( ppp_p, bp, "bad IP packet" );
  246.             ppp_p->OutError++;
  247.             return -1;
  248.         };
  249.     }
  250.     return (*ifp->output)(ifp,NULLCHAR,NULLCHAR,protocol,bp);
  251. }
  252.  
  253.  
  254. /* Send a packet with PPP header */
  255. int
  256. ppp_output(
  257.     struct iface *ifp,        /* Pointer to interface control block */
  258.     char *dest,                    /* Dest addr (ignored; PPP is point-to-point) */
  259.     char *source,                /* Source addr (ignored; PPP is point-to-point) */
  260.     int16 protocol,            /* PPP Protocol Type field */
  261.     struct mbuf *data )        /* Actual data to be sent */
  262. {
  263.     struct ppp_s *ppp_p;
  264.     struct mbuf *bp;
  265.     struct ppp_hdr hdr;
  266.  
  267.     if (ifp == NULLIF
  268.      || (ppp_p = ifp->edv) == NULL) {
  269.         free_p(data);
  270.         return -1;
  271.     }
  272.  
  273.     if (ppp_p->phase == pppDEAD) {
  274.         ppp_error( ppp_p, data, "line not up" );
  275.         ppp_p->OutError++;
  276.         return -1;
  277.     }
  278.  
  279.     /* Reset the idle timer if necessary. */
  280.  
  281.     reset_idle (ppp_p, protocol);
  282.     
  283.     hdr.addr = HDLC_ALL_ADDR;
  284.     hdr.control = HDLC_UI;
  285.     hdr.protocol = protocol;
  286.  
  287.     if ((bp = htonppp(&hdr, data)) == NULLBUF) {
  288.         ppp_log( ppp_p, Nospace );
  289.         ppp_p->OutMemory++;
  290.         return -1;
  291.     }
  292.  
  293.     return (*ifp->raw)(ifp,bp);
  294. }
  295.  
  296.  
  297. /* Encode a raw packet in PPP framing, put on link output queue */
  298. static int
  299. ppp_raw(ifp,bp)
  300. struct iface *ifp;
  301. struct mbuf *bp;
  302. {
  303.     struct ppp_s *ppp_p = ifp->edv;
  304.     struct lcp_s *lcp_p = ppp_p->fsm[Lcp].pdv;
  305.     int full_lcp, full_ac, full_p;
  306.     int16 calc_fcs = HDLC_FCS_START;
  307.     int32 accm = LCP_ACCM_DEFAULT;
  308.     struct ppp_hdr ph;
  309.     int len = PPP_HDR_LEN;
  310.     struct mbuf *vbp;
  311.     register char *cp;
  312.     register int c;
  313.  
  314.     dump(ifp,IF_TRACE_OUT,CL_PPP,bp);
  315.     ppp_p->OutTxOctetCount += len_p(bp) + 2;  /* count FCS bytes */
  316.     ifp->rawsndcnt++;
  317.     ifp->lastsent = secclock();
  318.     
  319.     /* Get the HDLC/PPP header, without actually pulling mbuf up */
  320.     if ( bp == NULLBUF || bp->cnt < PPP_HDR_LEN ) {
  321.         ppp_error( ppp_p, bp, "link header missing" );
  322.         ppp_p->OutError++;
  323.         return -1;
  324.     }
  325.  
  326.     ph.addr = bp->data[0];
  327.     ph.control = bp->data[1];
  328.     ph.protocol = get16(&bp->data[2]);
  329.  
  330.     if ( ( full_lcp = (ph.protocol == PPP_LCP_PROTOCOL) ) == 0
  331.      && (lcp_p->remote.work.negotiate & LCP_N_ACCM) ) {
  332.         accm = lcp_p->remote.work.accm;
  333.     }
  334.  
  335.     if ( ( full_p = ( full_lcp
  336.             ||  !(lcp_p->remote.work.negotiate & LCP_N_PFC)
  337.             ||  ph.protocol >= 0x00ff ) )
  338.         == 0 ) {
  339.         --len;
  340.     }
  341.  
  342.     /* Discard HDLC address and control fields if possible */
  343.     if ( ( full_ac = ( full_lcp
  344.             ||  !(lcp_p->remote.work.negotiate & LCP_N_ACFC) ) )
  345.         == 0 ) {
  346.         len -= 2;
  347.     }
  348.  
  349.     /* Load header with proper values */
  350.     bp->cnt -= PPP_HDR_LEN - len;
  351.     cp = (bp->data += PPP_HDR_LEN - len);
  352.  
  353.     if ( full_ac ) {
  354.         *cp++ = ph.addr;
  355.         *cp++ = ph.control;
  356.     }
  357.     if ( full_p )
  358.         *cp++ = (ph.protocol >> 8);
  359.     *cp++ = (ph.protocol & 0x00ff);
  360.  
  361.     /* Allocate output mbuf that's twice as long as the packet.
  362.      * This is a worst-case guess (consider a packet full of HDLC_FLAGs!)
  363.      */
  364.     if ((vbp = alloc_mbuf((int16)(2*len_p(bp) + HDLC_ENVLEN))) == NULLBUF) {
  365.         ppp_error( ppp_p, bp, Nospace );
  366.         ppp_p->OutMemory++;
  367.         return -1;
  368.     }
  369.     cp = vbp->data;
  370. #ifndef ATARI                                        /* No dma on ATARI                    */
  371.     /* No need to send an opening flag if the previous packet is still
  372.      * being transmitted.
  373.      */
  374.     if ( Asy[ifp->dev].dma.flags == 0 ) {
  375.         /* Flush out any line garbage */
  376.         *cp++ = HDLC_FLAG;
  377.         ppp_p->OutOpenFlag++;
  378.     }
  379. #endif    
  380.  
  381.     /* Copy input to output, escaping special characters */
  382.     while ((c = PULLCHAR(&bp)) != -1) {
  383.         /* Fold char value into FCS calculated so far */
  384.         calc_fcs = pppfcs(calc_fcs, c);
  385.  
  386.         if ( ((c < SP_CHAR) && (accm & (1L << c)))
  387.             || (c == HDLC_ESC_ASYNC)
  388.             || (c == HDLC_FLAG)) {
  389.             *cp++ = HDLC_ESC_ASYNC;
  390.             *cp++ = (c ^ HDLC_ESC_COMPL);
  391.         } else {
  392.             *cp++ = c;
  393.         }
  394.     }
  395.  
  396.     /* Final FCS calculation */
  397.     calc_fcs ^= 0xffff;
  398.     c = (calc_fcs & 0x00ff);    /* Least significant byte first */
  399.     if ( ((c < SP_CHAR) && (accm & (1L << c)))
  400.          ||(c == HDLC_ESC_ASYNC)
  401.          ||(c == HDLC_FLAG)) {
  402.         *cp++ = HDLC_ESC_ASYNC;
  403.         *cp++ = (c ^ HDLC_ESC_COMPL);
  404.     } else {
  405.         *cp++ = c;
  406.     }
  407.     c = (calc_fcs >> 8);        /* Most significant byte next */
  408.     if ( ((c < SP_CHAR) && (accm & (1L << c)))
  409.          ||(c == HDLC_ESC_ASYNC)
  410.          ||(c == HDLC_FLAG)) {
  411.         *cp++ = HDLC_ESC_ASYNC;
  412.         *cp++ = (c ^ HDLC_ESC_COMPL);
  413.     } else {
  414.         *cp++ = c;
  415.     }
  416.  
  417.     /* Tie off the packet */
  418.     *cp++ = HDLC_FLAG;
  419.     vbp->cnt = cp - vbp->data;
  420.  
  421.     if (ifp->trace & IF_TRACE_RAW)
  422.         raw_dump(ifp,IF_TRACE_OUT,vbp);
  423.     return asy_send(ifp->dev,vbp);
  424. }
  425.  
  426.  
  427. /****************************************************************************/
  428. /* Packetize PPP input from device */
  429. /* (process started by ppp_init) */
  430.  
  431. void __stdargs ppp_recv(dev,p1,p2)
  432. int dev;
  433. void *p1;
  434. void *p2;
  435. {
  436.     struct iface *ifp = p1;
  437.     struct ppp_s *ppp_p = ifp->edv;
  438.     int32 accm = LCP_ACCM_DEFAULT;
  439.     int16 calc_fcs = HDLC_FCS_START;
  440.     struct mbuf *raw_bp = NULLBUF;
  441.     struct mbuf *head_bp = NULLBUF;
  442.     register struct mbuf *tail_bp = NULLBUF;
  443.     char *cp;            /* next byte in tail mbuf */
  444.     register int mode = FALSE;
  445.     register int c;
  446.  
  447.     while ( (c = get_asy(dev)) != -1 ) {
  448. #ifdef PPP_DEBUG_RAW
  449.         if (ifp->trace & IF_TRACE_RAW) {
  450.             if ( raw_bp != NULLBUF
  451.               || (raw_bp = alloc_mbuf( LCP_MRU_HI * 2 )) != NULLBUF ) {
  452.                 *raw_bp->data++ = c;
  453.                 raw_bp->cnt++;
  454.                 if ( raw_bp->cnt != 1 && c == HDLC_FLAG ) {
  455.                     raw_bp->data = (char *)(raw_bp + 1);
  456.                     raw_dump( ifp, IF_TRACE_IN, raw_bp );
  457.                     raw_bp->cnt = 0;
  458.                 }
  459.             }
  460.         }
  461. #endif
  462.         if ( c == HDLC_FLAG ) {
  463.             if ( mode & PPP_ESCAPED ) {
  464.                 ppp_skipped( ppp_p, head_bp,
  465.                     "deliberate cancellation" );
  466.                 ppp_p->InFrame++;
  467.             } else if ( mode & PPP_TOSS ) {
  468.                 free_p( head_bp );
  469.             } else if ( head_bp != NULLBUF ) {
  470.                 if ( calc_fcs != HDLC_FCS_FINAL ) {
  471.                     ppp_skipped( ppp_p, head_bp,
  472.                         "checksum error" );
  473.                     ppp_p->InChecksum++;
  474.                 } else {
  475.                     /* trim off FCS bytes */
  476.                     trim_mbuf(&head_bp, len_p(head_bp)-2);
  477.  
  478.                     net_route( ifp, CL_PPP, head_bp );
  479.                 }
  480.             } else {
  481.                 ppp_p->InOpenFlag++;
  482.             }
  483.  
  484.             /* setup for next buffer */
  485.             mode = FALSE;
  486.             head_bp = tail_bp = NULLBUF;
  487.             calc_fcs = HDLC_FCS_START;
  488.             accm = LCP_ACCM_DEFAULT;
  489.  
  490.             /* Use negotiated values if LCP finished */
  491.             if (ppp_p->fsm[Lcp].state == fsmOPENED) {
  492.                 struct lcp_s *lcp_p = ppp_p->fsm[Lcp].pdv;
  493.  
  494.                 if (lcp_p->local.work.negotiate & LCP_N_ACCM) {
  495.                     accm = lcp_p->local.work.accm;
  496.                 }
  497.             }
  498. #ifdef PPP_DEBUG_RAW
  499.             if (!(ifp->trace & IF_TRACE_RAW)) {
  500.                 if ( raw_bp != NULLBUF ) {
  501.                     free_p( raw_bp );
  502.                     raw_bp = NULLBUF;
  503.                 }
  504.             }
  505. #endif
  506.             continue;
  507.         }
  508.  
  509.         /* We reach here for every byte inside a frame.
  510.          * (The order of the following tests is important.)
  511.          * Discard spurious control characters.
  512.          * Check for escape sequence.
  513.          * (Allow escaped escape.)
  514.          */
  515.         if ( c < SP_CHAR && (accm & (1L << c)) ) {
  516.             continue;
  517.         } else if ( mode & PPP_ESCAPED ) {
  518.             mode &= ~PPP_ESCAPED;
  519.             c ^= HDLC_ESC_COMPL;
  520.         } else if ( c == HDLC_ESC_ASYNC ) {
  521.             mode |= PPP_ESCAPED;
  522.             continue;
  523.         }
  524.  
  525.         /* We reach here with a byte for the buffer.
  526.          * Make sure there is room for it.
  527.          */
  528.         if ( tail_bp == NULLBUF ) {
  529.             if ((tail_bp = alloc_mbuf(PPP_ALLOC)) == NULLBUF) {
  530.                 ppp_skipped( ppp_p, tail_bp, Nospace );
  531.                 ppp_p->InMemory++;
  532.                 mode |= PPP_TOSS;
  533.                 continue;
  534.             }
  535.             head_bp = tail_bp;
  536.             cp = tail_bp->data;
  537.         } else if ( tail_bp->cnt >= tail_bp->size ) {
  538.             /* Current mbuf is full */
  539.             if ( (tail_bp->next = alloc_mbuf(PPP_ALLOC)) == NULLBUF ) {
  540.                 /* No memory, drop the whole packet */
  541.                 ppp_skipped( ppp_p, head_bp, Nospace );
  542.                 ppp_p->InMemory++;
  543.                 head_bp = NULLBUF;
  544.                 mode |= PPP_TOSS;
  545.                 continue;
  546.             }
  547.             tail_bp = tail_bp->next;
  548.             cp = tail_bp->data;
  549.         }
  550.  
  551.         /* Store the byte, increment counts */
  552.         *cp++ = c;
  553.         tail_bp->cnt++;
  554.         calc_fcs = pppfcs(calc_fcs, c);
  555.     }
  556.  
  557.     /* clean up afterward */
  558.     free_p(raw_bp);
  559.     free_p(head_bp);
  560.     ifp->rxproc = NULLPROC;
  561. }
  562.  
  563. #ifdef TURBOC_SWITCH_BUG
  564. #pragma option -G-
  565. #endif
  566.  
  567. /* Process incoming PPP packets */
  568. /* (called from network task) */
  569. void
  570. ppp_proc(ifp,bp)
  571. struct iface *ifp;
  572. struct mbuf *bp;
  573. {
  574.     struct ppp_s *ppp_p;
  575.     struct ipcp_s *ipcp_p;
  576.     struct ppp_hdr ph;
  577.     struct mbuf *hbp;
  578.     int16 negotiated = FALSE;
  579.  
  580.     if ( ifp == NULLIF ) {
  581.         log(-1, "ppp_proc: missing iface" );
  582.         return;
  583.     }
  584.     if ( bp == NULLBUF ) {
  585.         trace_log(ifp, "ppp_proc: missing buffer" );
  586.         return;
  587.     }
  588.  
  589.     ppp_p = ifp->edv;
  590.     ppp_p->InRxOctetCount += len_p(bp) + 2;   /* count FCS bytes */
  591.  
  592.     /* Use negotiated values if LCP finished */
  593.     if (ppp_p->fsm[Lcp].state == fsmOPENED) {
  594.         struct lcp_s *lcp_p = ppp_p->fsm[Lcp].pdv;
  595.  
  596.         negotiated = lcp_p->local.work.negotiate;
  597.     }
  598.  
  599.     /* HDLC address and control fields may be compressed out */
  600.     if ((byte_t)bp->data[0] != HDLC_ALL_ADDR) {
  601.         if (!(negotiated & LCP_N_ACFC)) {
  602.             ppp_skipped( ppp_p, bp, "missing ALL address" );
  603.             ppp_p->InFrame++;
  604.             return;
  605.         }
  606.     } else if ((byte_t)bp->data[1] != HDLC_UI) {
  607.         if (!(negotiated & LCP_N_ACFC)
  608.          || !(negotiated & LCP_N_PFC)) {
  609.             ppp_skipped( ppp_p, bp, "missing UI" );
  610.             ppp_p->InFrame++;
  611.             return;
  612.         }
  613.     } else {
  614.         /* skip address/control fields */
  615.         pull16(&bp);
  616.     }
  617.  
  618.     /* Initialize the expected header */
  619.     ph.addr = HDLC_ALL_ADDR;
  620.     ph.control = HDLC_UI;
  621.     ph.protocol = PULLCHAR(&bp);
  622.  
  623.     /* First byte of PPP protocol field may be compressed out */
  624.     if ( ph.protocol & 0x01 ) {
  625.         if (!(negotiated & LCP_N_PFC)) {
  626.             ppp_skipped( ppp_p, bp, "missing upper protocol byte" );
  627.             ppp_p->InFrame++;
  628.             return;
  629.         }
  630.     } else {
  631.         ph.protocol = (ph.protocol << 8) | PULLCHAR(&bp);
  632.  
  633.         /* Second byte of PPP protocol field must be odd */
  634.         if ( !(ph.protocol & 0x01) ) {
  635.             ppp_skipped( ppp_p, bp, "missing lower protocol byte" );
  636.             ppp_p->InFrame++;
  637.             return;
  638.         }
  639.     }
  640.  
  641.     /* Reset the idle timer if necessary. */
  642.  
  643.     reset_idle (ppp_p, ph.protocol);
  644.     
  645.     switch(ph.protocol) {
  646.     case PPP_IP_PROTOCOL:    /* Regular IP */
  647.         if ( ppp_p->fsm[IPcp].state != fsmOPENED ) {
  648.             ppp_error( ppp_p, bp, "not open for IP traffic" );
  649.             ppp_p->InError++;
  650.             break;
  651.         }
  652.         ip_route(ifp,bp,0);
  653.         break;
  654.  
  655.     case PPP_COMPR_PROTOCOL:    /* Van Jacobson Compressed TCP/IP */
  656.         if ( ppp_p->fsm[IPcp].state != fsmOPENED ) {
  657.             ppp_skipped( ppp_p, bp, "not open for Compressed TCP/IP traffic" );
  658.             ppp_p->InError++;
  659.             break;
  660.         }
  661.  
  662.         ipcp_p = ppp_p->fsm[IPcp].pdv;
  663.         if (!(ipcp_p->local.work.negotiate & IPCP_N_COMPRESS)) {
  664.             ppp_skipped( ppp_p, bp, "Compressed TCP/IP not enabled" );
  665.             ppp_p->InError++;
  666.             break;
  667.         }
  668.  
  669.         if ( slhc_uncompress(ipcp_p->slhcp, &bp) <= 0 ) {
  670.             ppp_error( ppp_p, bp, "Compressed TCP/IP packet error" );
  671.             ppp_p->InError++;
  672.             break;
  673.         }
  674.         ip_route(ifp,bp,0);
  675.         break;
  676.  
  677.     case PPP_UNCOMP_PROTOCOL:    /* Van Jacobson Uncompressed TCP/IP */
  678.         if ( ppp_p->fsm[IPcp].state != fsmOPENED ) {
  679.             ppp_skipped( ppp_p, bp, "not open for Uncompressed TCP/IP traffic" );
  680.             ppp_p->InError++;
  681.             break;
  682.         }
  683.  
  684.         ipcp_p = ppp_p->fsm[IPcp].pdv;
  685.         if (!(ipcp_p->local.work.negotiate & IPCP_N_COMPRESS)) {
  686.             ppp_skipped( ppp_p, bp, "Uncompressed TCP/IP not enabled" );
  687.             ppp_p->InError++;
  688.             break;
  689.         }
  690.  
  691.         if ( slhc_remember(ipcp_p->slhcp, &bp) <= 0 ) {
  692.             ppp_error( ppp_p, bp, "Uncompressed TCP/IP packet error" );
  693.             ppp_p->InError++;
  694.             break;
  695.         }
  696.         ip_route(ifp,bp,0);
  697.         break;
  698.  
  699.     case PPP_LCP_PROTOCOL:    /* Link Control Protocol */
  700.         ppp_p->InNCP[Lcp]++;
  701.         fsm_proc(&(ppp_p->fsm[Lcp]),bp);
  702.         break;
  703.  
  704.     case PPP_PAP_PROTOCOL:    /* Password Authenticate Protocol */
  705.         if (ppp_p->phase != pppAP
  706.          && ppp_p->phase != pppREADY) {
  707.             ppp_error( ppp_p, bp, "not ready for Authentication" );
  708.             ppp_p->InError++;
  709.             break;
  710.         }
  711.         ppp_p->InNCP[Pap]++;
  712.         pap_proc(&(ppp_p->fsm[Pap]),bp);
  713.         break;
  714.  
  715.     case PPP_IPCP_PROTOCOL:    /* IP Control Protocol */
  716.         if (ppp_p->phase != pppREADY) {
  717.             ppp_error( ppp_p, bp, "not ready for IPCP traffic" );
  718.             ppp_p->InError++;
  719.             break;
  720.         }
  721.         ppp_p->InNCP[IPcp]++;
  722.         fsm_proc(&(ppp_p->fsm[IPcp]),bp);
  723.         break;
  724.  
  725.     default:
  726.         if ( ppp_p->trace )
  727.             trace_log(ppp_p->iface, "%s PPP Unknown packet protocol: %x;",
  728.                 ppp_p->iface->name,
  729.                 ph.protocol);
  730.         ppp_p->InUnknown++;
  731.  
  732.         /* Build Protocol Reject packet:
  733.          * put the header back on ...
  734.          */
  735.         if ((hbp = htonppp(&ph,bp)) == NULLBUF){
  736.             ppp_log( ppp_p, Nospace );
  737.             ppp_p->InMemory++;
  738.             return;
  739.         }
  740.         /* ... then pull off the address and control fields ... */
  741.         pull16(&hbp);
  742.         /* ... and send it as an LCP packet */
  743.         fsm_send( &(ppp_p->fsm[Lcp]), PROT_REJ, 0, hbp );
  744.         break;
  745.     };
  746. }
  747.  
  748. #ifdef TURBOC_SWITCH_BUG
  749. #pragma option -G
  750. #endif
  751.  
  752. /************************************************************************/
  753.  
  754. /* Keep track of changes in I-O status */
  755. /* (called through iface iostatus vector) */
  756. static int
  757. ppp_iostatus( ifp, command, value )
  758. struct iface *ifp;
  759. int command;
  760. int32 value;
  761. {
  762.     struct ppp_s *ppp_p = ifp->edv;
  763.  
  764.     switch ( command ) {
  765.     case PARAM_UP:
  766.         ppp_log( ppp_p, "Physical layer up" );
  767.  
  768.         if ( ppp_p->phase == pppDEAD ) {
  769.             ppp_p->phase = pppLCP;
  770.         }
  771.         fsm_start( &(ppp_p->fsm[Lcp]) );
  772.         return 0;
  773.  
  774.     case PARAM_DOWN:
  775.         ppp_log( ppp_p, "Physical layer down" );
  776.  
  777.         fsm_down( &(ppp_p->fsm[Lcp]) );
  778.         ppp_p->phase = pppDEAD;
  779.         return 0;
  780.     };
  781.     return -1;
  782. }
  783.  
  784.  
  785. static int
  786. ppp_discard(ifp,bp)
  787. struct iface *ifp;
  788. struct mbuf *bp;
  789. {
  790.     struct ppp_s *ppp_p = ifp->edv;
  791.  
  792.     return fsm_send(&(ppp_p->fsm[Lcp]), DISCARD_REQ, 0, bp);
  793. }
  794.  
  795.  
  796. static int
  797. ppp_echo(ifp,bp)
  798. struct iface *ifp;
  799. struct mbuf *bp;
  800. {
  801.     struct ppp_s *ppp_p = ifp->edv;
  802.  
  803.     return fsm_send(&(ppp_p->fsm[Lcp]), ECHO_REQ, 0, bp);
  804. }
  805.  
  806.  
  807. /****************************************************************************/
  808. /* Initialize PPP control structures for a Point-to-Point interface */
  809. int
  810. ppp_init(ifp)
  811. struct iface *ifp;
  812. {
  813.     struct ppp_s *ppp_p;
  814.     char *ifn;
  815.  
  816.     ppp_p = callocw(1,sizeof(struct ppp_s));
  817.  
  818.     ifp->edv = ppp_p;
  819.     ifp->iostatus = ppp_iostatus;
  820.     ifp->raw = ppp_raw;
  821.     ifp->show = ppp_show;
  822.     ifp->echo = ppp_echo;
  823.     ifp->discard = ppp_discard;
  824.  
  825.     ppp_p->iface = ifp;
  826.     ppp_p->phase = pppDEAD;
  827.     ppp_p->idle_durn = 0L;                /* GT 10 Aug 92                        */
  828.     lcp_init(ppp_p);
  829.     pap_init(ppp_p);
  830.     ipcp_init(ppp_p);
  831.  
  832.     ifp->rxproc = newproc( ifn = if_name( ifp, " receive" ),
  833.             320, ppp_recv, ifp->dev, ifp, NULL, 0);
  834.     free(ifn);
  835.     return 0;
  836. }
  837.  
  838.  
  839. int
  840. ppp_free(ifp)
  841. struct iface *ifp;
  842. {
  843.     struct ppp_s *ppp_p = ifp->edv;
  844.     int fsmi;
  845.  
  846.     alert( ifp->rxproc, -1 );
  847.  
  848.     for ( fsmi = Lcp; fsmi < fsmi_Size; ) {
  849.         fsm_free( &(ppp_p->fsm[fsmi++]) );
  850.     }
  851.     free( ppp_p->peername );
  852.     free( ppp_p );
  853.     return 0;
  854. }
  855.  
  856.  
  857. /****************************************************************************
  858. *    reset_idle                                                                *
  859. *    If the incoming or outgoing packet is not an LCP control message        *
  860. *    reset the PPP idle timer.                                                *
  861. ****************************************************************************/
  862.  
  863. static void reset_idle(
  864.     struct ppp_s *ppp_p,
  865.     int16 protocol )
  866. {
  867.     if (protocol == PPP_LCP_PROTOCOL)
  868.         return;                            /* ignore LCP messages                */
  869.  
  870.     if (!done_set_time &&
  871.         protocol != PPP_IPCP_PROTOCOL &&
  872.         protocol != PPP_LCP_PROTOCOL &&
  873.         protocol != PPP_PAP_PROTOCOL)
  874.         try_set_time(0, NULL, ppp_p);
  875.  
  876.     if (ppp_p->idle_durn == 0L)
  877.         return;                            /* no idle timeout                    */
  878.  
  879.     set_timer (&(ppp_p->idle_timer), ppp_p->idle_durn);    /* reset timeout    */
  880.     start_timer (&(ppp_p->idle_timer));
  881.     }    /* static void reset_idle (ppp_p, ip_packet) */
  882.